<template>
  <div>
    <el-card>
      <template #header>
        <h4>添加账号</h4>
      </template>
      <el-form
        :model="addForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="account">
          <el-input
            type="text"
            v-model="addForm.account"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="addForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户组" prop="region">
          <el-select v-model="addForm.region" placeholder="请选择活动区域">
            <el-option label="普通管理员" value="普通管理员"></el-option>
            <el-option label="超级管理员" value="超级管理员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  validateAccount,
  validatePass,
  checkregion
} from '../../utils/validate.js'

import { accAdd } from '../../api/info.js'

export default {
  data() {
    return {
      addForm: {
        account: '',
        pass: '',
        region: ''
      },
      rules: {
        account: [{ validator: validateAccount, trigger: 'blur' }],
        pass: [{ validator: validatePass, trigger: 'blur' }],
        region: [{ validator: checkregion, trigger: 'change' }]
      }
    }
  },
  created() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const data = await accAdd({
            account: this.addForm.account,
            password: this.addForm.pass,
            userGroup: this.addForm.region
          })
          console.log(data)
          if (data.code === 0) {
            this.$router.push('/account/list')
          } else {
            this.$message.error('添加账号失败')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-input__inner {
  width: 300px;
}
</style>
